<template>
	<view class="content">
		<view class="">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="用户名/电话"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="密码"
				></wInput>
			</view>
			<wButton 
				text="登 录"
				:rotate="isRotate" 
				@click.native="startLogin()"
			></wButton>
			
			<!-- 其他登录 -->
			<!-- #ifdef MP-WEIXIN -->
			<view class="quick_login">
				<view class="t"><text class="title-wrap">微信快速登录</text></view>
			</view>
			<button class="quick_login_list" open-type="getUserInfo" @getuserinfo="getuserinfo" withCredentials="true">
				<text class="wechat"></text>
			</button>
			<!-- #endif -->
			<!-- #ifdef H5 -->
				<!--<view class="quick_login">
					<view class="t"><text class="title-wrap">微信快速登录</text></view>
				</view>
				<view class="quick_login_list">
					<text class="wechat" @click="thirdLogin('wechat')"></text>
				</view>-->
			<!-- #endif -->
			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text class="line">|</text>
				<navigator url="reg" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import {
		mpWechatLogin,
		authLogin,
	} from '@/api/login';
	
	import {  
	    mapMutations  
	} from 'vuex';
	export default {
		data() {
			return {
				logoImage:'/static/logo.jpg',
				phoneData:'', //用户/电话
				passData:'', //密码
				isRotate: false, //是否加载旋转
			};
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			_this= this;
			//this.isLogin();
		},
		methods: {
			...mapMutations(['login']),
			isLogin(){
				//判断缓存中是否登录过，直接登录
				// try {
				// 	const value = uni.getStorageSync('setUserData');
				// 	if (value) {
				// 		//有登录信息
				// 		console.log("已登录用户：",value);
				// 		_this.$store.dispatch("setUserData",value); //存入状态
				// 		uni.reLaunch({
				// 			url: '../../../pages/index',
				// 		});
				// 	}
				// } catch (e) {
				// 	// error
				// }
			},
			getuserinfo(e){
				let userInfo = e.detail.userInfo;
				this.$api.wxLogin().then(r=>{
					return this.$api.doLogin(userInfo, r.code);
				}).then(r=>{
					this.$api.getUser(r.username, r.sessionKey).then(user=>{
						this.isRotate=false;
						this.login(user);
						uni.navigateBack();  
					}).catch((message)=>{
						uni.showToast({
						    icon: 'none',
							position: 'bottom',
						    title: message
						});
					});
				});
			},
		    async startLogin(){
				//登录
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '用户名不能为空'
				    });
				    return;
				}
		        if (this.passData.length < 5) {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码格式不正确'
		            });
		            return;
		        }
				const result = await this.$api.json('userInfo');
				if(result.status === 1){
					this.login(result.data);
				    uni.navigateBack();  
				}else{
					this.$api.msg(result.msg);
					this.logining = false;
				}
				/* this.isRotate=true
				this.$api.webLogin(this.phoneData, this.passData).then((res)=>{
					this.isRotate=false;
					//获取用户信息
					this.$api.getUser(this.phoneData, res.body).then(user=>{
						this.isRotate=false;
						this.login(user);
						uni.navigateBack();  
					}).catch((message)=>{
						uni.showToast({
						    icon: 'none',
							position: 'bottom',
						    title: message
						});
					});
				}).catch((message)=>{
					this.isRotate=false;
					uni.showToast({
					    icon: 'none',
						position: 'bottom',
					    title: message
					});
				}); */
		    },
			thirdLogin(type) {
				this.$api.weixinLogin().then(res=>{
					console.log(res);
				});
			}
		}
	}
</script>

<style>
	page{overflow: hidden;}
	/* 头部 logo */
	.header {
		width:161upx;
		height:161upx;
		box-shadow:0upx 0upx 60upx 0upx rgba(0,0,0,0.1);
		border-radius:50%;
		margin-top: 80upx;
		margin-bottom: 50upx;
		margin-left: auto;
		margin-right: auto;
	}
	.header image{
		width:161upx;
		height:161upx;
		border-radius:50%;
	}
	/* 主体 */
	.main {
		display: flex;
		flex-direction: column;
		padding-left: 70upx;
		padding-right: 70upx;
	}
	.tips {
		color: #999999;
		font-size: 28upx;
		margin-top: 64upx;
		margin-left: 48upx;
	}
	
	/* 其他登录方式 */
	.other_login{
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		margin-top: 256upx;
		text-align: center;
	}
	.login_icon{
		border: none;
		font-size: 64upx;
		margin: 0 64upx 0 64upx;
		color: rgba(0,0,0,0.7)
	}
	.wechat_color{
		color: #83DC42;
	}
	.weibo_color{
		color: #F9221D;
	}
	.github_color{
		color: #24292E;
	}
	
	/* 底部 */
	.footer{
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		margin-top: 64upx;
		color: rgba(0,0,0,0.7);
		text-align: center;
		height: 40upx;
		line-height: 40upx;
	}
	.footer .t{
		margin-top: 3upx;
	}
	.footer .line{font-size: 24upx;
	margin-left: 15upx;
	margin-right: 15upx;}
	.quick_login{ padding:40upx 100upx 40upx; flex: 1;background: #fff; margin-top: 0upx; display: flex;}
	.quick_login .t {height: 24upx;border-bottom: 0px solid #ddd;text-align: center; width:100%; margin: 0 auto;}
	.quick_login .title-wrap {display: inline-block;height:32upx;padding: 0 20upx;color: #000;line-height: 32upx; font-size:32upx; font-weight:normal; background:#fff; }
	.quick_login_list{text-align: center;margin-top: 20upx;background: #fff;border:none}
	uni-button:after,button:after{ display: none;}
	.quick_login_list .wechat{ display: inline-block;width:120upx; height: 120upx;border:#2dbb5f solid 1px; border-radius: 50%; position: relative;}
	.quick_login_list .wechat:after{font-family:iconfont ;font-size: inherit;line-height: 1;position: absolute;top: 50%;display: inline-block;-webkit-transform: translateY(-50%);
	transform: translateY(-50%);text-decoration: none;color: #2dbb5f;-webkit-font-smoothing: antialiased;right: 15px;font-size: 60upx;content: '\e604';}
</style>